package com.hdrx.fundhelper.service.userFund;

import com.hdrx.fundhelper.dao.dto.FundIncome;
import com.hdrx.fundhelper.mapper.UserFundMapper;
import com.hdrx.fundhelper.mapper.UserInfoMapper;
import com.hdrx.fundhelper.dao.pojo.UserFundRelationship;
import com.hdrx.fundhelper.dao.vo.UserIncomeVO;
import com.hdrx.fundhelper.dao.pojo.Userinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: houdx
 * @description:
 * @date: 2023/6/19 15:18
 */
@Service
@Slf4j
public class UserFundService {

    @Autowired
    UserFundMapper userFundMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    public void addUserFund(UserFundRelationship userFundRelationship) {
        try {
            userFundMapper.insert(userFundRelationship);
        } catch (Exception e) {
            log.error("新增用户持仓信息失败{}", e.getMessage());
            throw e;
        }

    }

    public UserIncomeVO queryUserIncome(Integer id) {
        UserIncomeVO userIncomeVO = new UserIncomeVO();

        List<FundIncome> fundIncomeList = new ArrayList<>();

        //查询用户信息
        Userinfo userinfo = userInfoMapper.selectByPrimaryKey(id);


        userIncomeVO.setUserId(userinfo.getUserId()).setUserName(userinfo.getUserName());

        //用户次用基金信息
        Example example = new Example(UserFundRelationship.class);
        Example.Criteria and = example.and();
        and.andEqualTo("userId",userinfo.getUserId());
        List<UserFundRelationship> userFundRelationships = userFundMapper.selectByExample(example);
        BigDecimal sum0 = new BigDecimal(0);
        userFundRelationships.forEach(e -> {
            FundIncome fundIncome = new FundIncome();
            fundIncome.setFundCode(e.getFundCode());
            fundIncome.setFundName(e.getFundName());
            fundIncome.setFundIncome((e.getLatestPrice().subtract(e.getLastTradingDayPrice())).multiply(e.getFundShare()));
            fundIncome.setTotalIncome((e.getLatestPrice().subtract(e.getHoldingCost())).multiply(e.getFundShare()));
            fundIncome.setYieldRate(((e.getLatestPrice().subtract(e.getHoldingCost())).divide(e.getHoldingCost(),10, RoundingMode.HALF_UP)).multiply(new BigDecimal(100))+"%");
            fundIncomeList.add(fundIncome);

        });

        BigDecimal sum = new BigDecimal(0);
        BigDecimal sum1 = new BigDecimal(0);
        for (int i = 0; i < fundIncomeList.size(); i++) {
            sum = sum.add(fundIncomeList.get(i).getTotalIncome());
            sum1 = sum1.add(fundIncomeList.get(i).getFundIncome());
            sum0 = sum0.add(userFundRelationships.get(i).getFundShare().multiply(userFundRelationships.get(i).getHoldingCost()));
        }
        userIncomeVO.setAllYieldRate(sum.divide(sum0,10,RoundingMode.HALF_UP)+"%");
        userIncomeVO.setAllIncome(sum);
        userIncomeVO.setTodayAllIncome(sum1);
        userIncomeVO.setFundIncomeList(fundIncomeList);
        return userIncomeVO;
    }
}
